package com.wg.wuge.controller;

import com.lark.oapi.core.utils.Jsons;
import com.lark.oapi.event.EventDispatcher;
import com.lark.oapi.sdk.servlet.ext.ServletAdapter;
import com.lark.oapi.service.contact.ContactService;
import com.lark.oapi.service.contact.v3.model.P2UserCreatedV3;
import com.lark.oapi.service.im.ImService;
import com.lark.oapi.service.im.v1.model.P1MessageReadV1;
import com.lark.oapi.service.im.v1.model.P2MessageReadV1;
import com.lark.oapi.service.im.v1.model.P2MessageReceiveV1;
import com.wg.wuge.domain.EncryptRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
public class TestController {

    //1. 注册消息处理器
    private final EventDispatcher EVENT_DISPATCHER = EventDispatcher.newBuilder("oqIQHsE0HVfFV6tjrkraQdRJhyWA1WtU",
                    "5t7j5H0m2h27svoTwZxRZuXQexsucra6")
            .onP2MessageReceiveV1(new ImService.P2MessageReceiveV1Handler() {
                @Override
                public void handle(P2MessageReceiveV1 event) {
                    System.out.println(Jsons.DEFAULT.toJson(event));
                    System.out.println(event.getRequestId());
                }
            }).onP2UserCreatedV3(new ContactService.P2UserCreatedV3Handler() {
                @Override
                public void handle(P2UserCreatedV3 event) {
                    System.out.println(Jsons.DEFAULT.toJson(event));
                    System.out.println(event.getRequestId());
                }
            })
            .onP2MessageReadV1(new ImService.P2MessageReadV1Handler() {
                @Override
                public void handle(P2MessageReadV1 event) {
                    System.out.println(Jsons.DEFAULT.toJson(event));
                    System.out.println(event.getRequestId());
                }
            }).onP1MessageReadV1(new ImService.P1MessageReadV1Handler() {
                @Override
                public void handle(P1MessageReadV1 event) {
                    System.out.println(Jsons.DEFAULT.toJson(event));
                    System.out.println(event.getRequestId());
                }
            })
            .build();

    //2. 注入 ServletAdapter 实例
    @Autowired
    private ServletAdapter servletAdapter;

    //3. 创建路由处理器
    @RequestMapping("/webhook/event")
    public String event(HttpServletRequest request, HttpServletResponse response,
                        @RequestBody EncryptRequest encrypt)
            throws Throwable {
//        String decrypt = Decrypt.decrypt(encrypt.getEncrypt());
//        Decrypt decrypt = new Decrypt("6T6256rJNvKeD2iLtq2X9dNMqb6SnlXJ");
//        decrypt.calculateSignature()

        //3.1 回调扩展包提供的事件回调处理器
//        servletAdapter.handleEvent(request, response, EVENT_DISPATCHER);
        String s = EVENT_DISPATCHER.decryptEvent(encrypt.getEncrypt());
        System.out.println(s);
        return s;
    }
}
